package com.example.Opd_ISF_Android_Application.view.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.example.Opd_ISF_Android_Application.R;
import com.example.Opd_ISF_Android_Application.model.Ranking;
import com.example.Opd_ISF_Android_Application.view.adapters.RankingAdapter;

import java.nio.BufferUnderflowException;

/**
 * Created with IntelliJ IDEA.
 * User: Dell
 * Date: 26/03/13
 * Time: 14:08
 * To change this template use File | Settings | File Templates.
 */
public class RankingActivity extends Activity {
    private ListView rankingList;
    private RankingAdapter data;
    private Ranking comparisonItem1;
    private Ranking comparisonItem2;
    private String type;
    private TextView item1, item2;
    private Button btnCompare;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
        setContentView(R.layout.ranking);

        // initialise components & list
        initialiseComponents();
        addEvents();
    }

    private void initialiseComponents() {
        rankingList = (ListView) findViewById(R.id.rankingList);
        item1 = (TextView) findViewById(R.id.tvItem1);
        item2 = (TextView) findViewById(R.id.tvItem2);
        btnCompare = (Button) findViewById(R.id.btnCompare);

        if (isStudentRanking()) {

            //TODO: getter van model die dingen ophaalt van web
            Ranking[] r =
                    {
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "9000")

                    };
            data = new RankingAdapter(this, R.layout.ranking_custom_list_item, r);
        } else {
            //TODO: getter van model die dingen ophaalt van web
            Ranking[] r =
                    {
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000"),
                            new Ranking("Mo Binni", "BEL", "FLANDERS", "8000")

                    };
            data = new RankingAdapter(this, R.layout.ranking_custom_list_item, r);
        }
        rankingList.setAdapter(data);

    }

    private void addEvents() {
        // Eventhandler for when someone clicks on a list item
        rankingList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (comparisonItem1 == null) {
                    comparisonItem1 = getRanking(view);
                    item1.setText(comparisonItem1.getName());

                } else {
                    if (comparisonItem2 == null) {
                        comparisonItem2 = getRanking(view);
                        item2.setText(comparisonItem2.getName());
                    } else {
                        confirmationDialog();

                    /*  Confirmation dialog is shown when someone tries to choose a third item
                        It will give the user the option to reset his chosen students/schools  */
                    }
                }
                return true;
            }
        });

        // Event handler for when someone clicks on the compare button
        btnCompare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 /*  Check if the comparison items have a value, if not you cannot go to the comparison activity  */
                if (comparisonItem1 != null && comparisonItem2 != null) {
                    Intent compareActivity = new Intent(getResources().getString(R.string.compareIntent));
                    // add data to send to activity
                    compareActivity.putExtra("item 1", comparisonItem1.getName());
                    compareActivity.putExtra("item 2", comparisonItem2.getName());
                    startActivity(compareActivity);
                    finish();
                } else {
                    Toast.makeText(getBaseContext(), String.format("You have to select 2 %ss first!", type), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /*  Confirmation dialog method that shows the dialog and does the necessary adjustments
     *  to the fields if the user chooses to reset */
    private void confirmationDialog() {
        String warning = String.format("You have already chosen 2 %ss. Would you like to reset your choices?", type);
        new AlertDialog.Builder(this)
                .setTitle("Error")
                .setMessage(warning)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        comparisonItem1 = null;
                        comparisonItem2 = null;
                        item1.setText(type + " 1");
                        item2.setText(type + " 2");
                    }
                })
                .setNegativeButton(android.R.string.no, null).show();
    }

    /*  Get a ranking object based on the values of a given view */
    private Ranking getRanking(View view) {
        Ranking ranking = new Ranking();
        ranking.setName(((TextView) view.findViewById(R.id.tvRankingName)).getText().toString());
        ranking.setCountry(((TextView) view.findViewById(R.id.tvRankingCountry)).getText().toString());
        ranking.setRegion(((TextView) view.findViewById(R.id.tvRankingRegion)).getText().toString());
        ranking.setScore(((TextView) view.findViewById(R.id.tvRankingScore)).getText().toString());
        return ranking;
    }


    private ArrayAdapter retrieveListOfItems() {
        Ranking[] r = null;
        data = new RankingAdapter(this, R.layout.ranking_custom_list_item, r);
        return data;
        /*
         * This method will call a method from a model class that does the HTTP Request and
         * returns the JSON data in an arraylist or array of ranking objects that will be placed
         * in the arrayadapter above
        */
    }


    /*  Check if you're handling a student ranking based on data received */
    private boolean isStudentRanking() {
        Bundle extras = getIntent().getExtras();
        boolean ranking = false;
        if (extras != null) ranking = extras.getBoolean("studentRanking");
        if (ranking) {
            type = "Student";
            return true;
        } else {
            type = "School";
            item1.setText("School 1");
            item2.setText("School 2");
            return false;
        }
    }


}
